﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// sellmanclient 的摘要说明。
    /// </summary>
    public partial class SellManClientPage : Page
    {
        private DateTime begintime;
        private DateTime endtime;
        private int sellmanid;
        private string type;

        protected static string GetCurStatus(string curstatus)
        {
            switch (curstatus)
            {
                case "trace":
                    return ("跟踪");
                case "boot":
                    return ("启动");
                case "commend":
                    return ("产品推荐");
                case "requirement":
                    return ("需求定义");
                case "submit":
                    return ("方案提交");
                case "negotiate":
                    return ("商务谈判");
                case "actualize":
                    return ("项目实施");
                case "traceservice":
                    return ("跟踪服务");
                case "last":
                    return ("收尾款");
            }
            return ("");
        }

        protected static string GetAddMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["realname"].ToString());
            }
            return ("");
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                sellmanid = (Request.QueryString["Sellmanid"] == null) ? 0 : Int32.Parse(Request.QueryString["Sellmanid"]);
                begintime = (Request.QueryString["begintime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["begintime"]);
                endtime = (Request.QueryString["endtime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["endtime"]);
                type = (Request.QueryString["type"] == null) ? "" : Request.QueryString["type"];
                BindData();
            }

            base.OnLoad(e);
        }

        private void BindData()
        {
            var cm = new Components.CM();
            SqlDataReader dr_client = null;
            if (type == "all")
            {
                dr_client = cm.GetNewClientBySellman(sellmanid, begintime, endtime);
            }
            else if (type == "3stars")
            {
                dr_client = cm.GetNew3StarClientBySellmanID(sellmanid, begintime, endtime);
            }

            var ds = new DataSet();
            var staff = new Components.Staff();
            SqlDataReader dr_Staff = staff.GetAllStaffs();
            DataTable dt_Staff = Tools.ConvertDataReaderToDataTable(dr_Staff);
            dt_Staff.TableName = "Staff";
            ds.Tables.Add(dt_Staff);

            DataTable dt_client = Tools.ConvertDataReaderToDataTable(dr_client);
            dt_client.TableName = "Client";
            ds.Tables.Add(dt_client);

            ds.Relations.Add("ClientAddMan_Staff", ds.Tables["Client"].Columns["AddManID"], ds.Tables["Staff"].Columns["Staff_ID"], false);
            dgrd_sellmanclient.DataSource = dt_client.DefaultView;
            dgrd_sellmanclient.DataBind();
        }
    }
}